#include "calcmain.h"
#include "./ui_calcmain.h"

#include "calc_core.h"
#include <qnamespace.h>
#include <qobject.h>
#include <variant>

CalcMain::CalcMain(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::CalcMain) {
  ui->setupUi(this);
}

CalcMain::~CalcMain() { delete ui; }

void CalcMain::on_calculate_button_clicked() {
  QString expression = ui->expression_input->text();
  std::variant<double, QString> result;
  ExpressionParser::parse(expression, result);
  if (std::holds_alternative<QString>(result)) {
    ui->result_output->setText(std::get<QString>(result));
  } else {
    ui->result_output->setText(QString::number(std::get<double>(result)));
  }
}

void CalcMain::on_clear_expression_button_clicked() {
  ui->expression_input->setText("");
}

void CalcMain::on_clear_result_button_clicked() {
  ui->result_output->setText("");
}

void CalcMain::on_expression_input_returenPressed() {
    on_calculate_button_clicked();
}

void CalcMain::on_button_1_clicked() {
  ui->expression_input->setText(ui->expression_input->text() + '1');
}

void CalcMain::on_button_2_clicked() {
  ui->expression_input->setText(ui->expression_input->text() + '2');
}

void CalcMain::on_button_3_clicked() {
  ui->expression_input->setText(ui->expression_input->text() + '3');
}

void CalcMain::on_button_4_clicked() {
  ui->expression_input->setText(ui->expression_input->text() + '4');
}

void CalcMain::on_button_5_clicked() {
  ui->expression_input->setText(ui->expression_input->text() + '5');
}

void CalcMain::on_button_6_clicked() {
  ui->expression_input->setText(ui->expression_input->text() + '6');
}

void CalcMain::on_button_7_clicked() {
  ui->expression_input->setText(ui->expression_input->text() + '7');
}

void CalcMain::on_button_8_clicked() {
  ui->expression_input->setText(ui->expression_input->text() + '8');
}

void CalcMain::on_button_9_clicked() {
  ui->expression_input->setText(ui->expression_input->text() + '9');
}

void CalcMain::on_button_0_clicked() {
  ui->expression_input->setText(ui->expression_input->text() + '0');
}

void CalcMain::on_button_point_clicked() {
  ui->expression_input->setText(ui->expression_input->text() + '.');
}

void CalcMain::on_button_del_clicked() {
  QString str = ui->expression_input->text();
  ui->expression_input->setText(
      str.left((str.length() > 0 ? str.length() : 1) - 1));
}

void CalcMain::on_button_add_clicked() {
  ui->expression_input->setText(ui->expression_input->text() + '+');
}

void CalcMain::on_button_sub_clicked() {
  ui->expression_input->setText(ui->expression_input->text() + '-');
}

void CalcMain::on_button_mul_clicked() {
  ui->expression_input->setText(ui->expression_input->text() + '*');
}

void CalcMain::on_button_div_clicked() {
  ui->expression_input->setText(ui->expression_input->text() + '/');
}

void CalcMain::on_button_leftbrace_clicked() {
  ui->expression_input->setText(ui->expression_input->text() + '(');
}

void CalcMain::on_button_rightbrace_clicked() {
  ui->expression_input->setText(ui->expression_input->text() + ')');
}